import Vue from "vue";
import Vuex from "vuex";

import { Config } from "../utils/typechecker";

Vue.use(Vuex);

export const STORAGE_KEY = {
	TABLE_FULLWIDTH: "$MATABLE_CONFIG_TABLEFULLWIDTH",
};

export type ITableData = Array<{ [p: string]: string }>;

export interface IStore {
	data: {
		[p: string]: ITableData;
	};
	config: Config[];
	index: number;
	globalConfig: {
		title?: string;
		countdown?: {
			name: string;
			date: string;
		};
		copyright: boolean;
		tableFullWidth: boolean;
	};
}

export function newStore() {
	return new Vuex.Store<IStore>({
		state: {
			data: {},
			config: [],
			index: 0,
			globalConfig: {
				title: document.title,
				countdown: undefined,
				copyright: true,

				tableFullWidth:
					0 < Number(window.localStorage.getItem(STORAGE_KEY.TABLE_FULLWIDTH)),
			},
		},
		mutations: {
			setTableFullWidth(state: IStore, val: boolean) {
				state.globalConfig.tableFullWidth = val;
			},
		},
		modules: {},
	});
}
